package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.AddressDao;
import com.itheima.entity.PageResult;
import com.itheima.pojo.Address;
import com.itheima.pojo.Order;
import com.itheima.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.JedisPool;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
地址相关服务实现类
 */
@Service(interfaceClass = AddressService.class)
@Transactional
public class AddressServiceImpl implements AddressService {

    @Autowired //自动注入bean
    private AddressDao addressDao;

    //注入spring-redis 使用JedisPool操作Redis服务
    @Autowired
    public JedisPool jedisPool;

    //分页查询
    @Override
    public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString) {

        Page<Address> addresses;

        PageHelper.startPage(currentPage, pageSize);
        addresses=   addressDao.selectByCondition(queryString);


        long total = addresses.getTotal();
        List<Address> rows = addresses.getResult();

        return new PageResult(total, rows);
    }

    //删除
    @Override
    public void deleteById(Integer id) {
        if (id < 0 || id == null) {
            return;
        }
        //删中间表再删主表
        addressDao.deleteCenter(id);
        addressDao.deleteById(id);
    }

//修改
    @Override
    public void edit(Address address) {

        //3.再调用修改方法,更新 新的关联数据*/
        addressDao.edit(address);
    }

//添加
    @Override
    public void add(Address address) {
        //要进行数据合法性校验
        addressDao.add(address);
        //将图片名称保存到Redis集合中
        String fileName = address.getImg();
        //传入小集合中 也就是真正传输到redis的图片
        jedisPool.getResource().sadd("addressPicDbResources",fileName);
    }


    //根据id查询
    @Override
    public Address findById(Integer id){
        return   addressDao.findById(id);
    }

    //查询地图
    @Override
    public List<String> findAllmap() {
        return   addressDao.findAllmap();
    }

    //查找所有地址相关信息
    @Override
    public List<Map<String, Object>> findAllAddress() {
        return addressDao.findAllAddress();
    }


/* //设置体检中心和预约表的关联关系的方法
    public void setAddressAndOrder(Integer addressId, Integer[] orderIds) {
        //判断不为空
        if (orderIds != null && orderIds.length > 0) {
            //遍历取出每一个选定的id
            for (Integer orderId : orderIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("addressId_id", addressId);
                map.put("orderId_id", orderId);
                //调用Dao层方法
                addressDao.setAddressAndOrder(map);
            }
        }
    }*/
}
